package com.pucheng.bracelet.ui.activity.sleep

import android.content.Intent
import android.os.Bundle
import android.widget.ProgressBar
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.pucheng.bracelet.R
import com.pucheng.bracelet.app.util.DatetimeUtil
import com.pucheng.bracelet.app.util.StatusBarUtil
import com.pucheng.bracelet.app.util.click
import com.pucheng.bracelet.data.bean.SleepBean
import com.pucheng.bracelet.ui.fragment.heartrate.AllDayHeartRateToDayFragment
import com.pucheng.bracelet.ui.fragment.sleep.SleepToDayFragment
import com.pucheng.bracelet.ui.fragment.sleep.SleepToMonthFragment
import com.pucheng.bracelet.ui.fragment.sleep.SleepToWeekFragment
import com.pucheng.bracelet.ui.view.CircleProgressView
import com.pucheng.bracelet.viewmodel.sleep.SleepViewModel
import com.pucheng.bracelet.viewmodel.steps.StepsViewModel
import kotlinx.android.synthetic.main.activity_sleep.*
import kotlinx.android.synthetic.main.activity_sleep.layout_title
import kotlinx.android.synthetic.main.activity_sleep.rv_to7day
import kotlinx.android.synthetic.main.activity_steps.*
import kotlinx.android.synthetic.main.include_viewpager_sleep.*
import kotlinx.android.synthetic.main.layout_title.*
import me.hgj.jetpackmvvm.base.activity.BaseVmActivity
import me.hgj.jetpackmvvm.demo.app.ext.bindViewPager2
import me.hgj.jetpackmvvm.demo.app.ext.initToActivity

class SleepActivity : BaseVmActivity<SleepViewModel>() {

    private lateinit var sleepTo7DayAdp: BaseQuickAdapter<SleepBean, BaseViewHolder>

    override fun createObserver() {
        mViewModel.apply {
            sleepDataInfo.observe(this@SleepActivity, Observer {
                if (it != null) {
                    sleepData.value = it

                }
            })
            sleepData.observe(this@SleepActivity, Observer {
                pg_restful.setMax(it.totalTime)
                pg_restful.setProgress(it.restfulTime)
                pg_light.setMax(it.totalTime)
                pg_light.setProgress(it.lightTime)
                pg_active.setMax(it.totalTime)
                pg_active.setProgress(it.activeTime)
            })
            if (sleepData.value == null) {
                sleepTo7Day.observe(this@SleepActivity, Observer {
                    if (!it.isNullOrEmpty()) {
                        it.sortedBy { it1 -> it1.date }
                        sleepData.value = it.last()
                        sleepTo7DayAdp = object :
                            BaseQuickAdapter<SleepBean, BaseViewHolder>(
                                R.layout.item_sleep_7day_data,
                                it as MutableList<SleepBean>?
                            ) {
                            override fun convert(holder: BaseViewHolder, item: SleepBean) {
                                var pg = holder.getView<ProgressBar>(R.id.pg_sleep_data)
                                pg.max = item.totalTime
                                pg.progress = item.restfulTime
                                pg.secondaryProgress = item.lightTime
                                holder.setText(
                                    R.id.tv_date,
                                    DatetimeUtil.formatDate(item.timestamp, DatetimeUtil.DATE_MD)
                                )
                            }

                        }
                        rv_to7day.adapter = sleepTo7DayAdp
                    }
                })
            }
        }
    }

    override fun dismissLoading() {
    }

    override fun initView(savedInstanceState: Bundle?) {
        StatusBarUtil.setColor(this, resources.getColor(R.color.sleep_bg), 0)
        tv_title.text = getString(R.string.sleep)
        ibtn_back.click { finish() }
        ibtn_right.click { startActivity(Intent(this, SleepHistoryActivity::class.java)) }
        layout_title.setBackgroundColor(resources.getColor(R.color.sleep_bg))
        magic_indicator.setBackgroundResource(R.color.sleep_bg)
        //标题集合
        var mDataList: ArrayList<String> = arrayListOf()
        mDataList.add("日")
        mDataList.add("周")
        mDataList.add("月")
        var fragments: ArrayList<Fragment> = arrayListOf()
        fragments.add(SleepToDayFragment.newInstance())
        fragments.add(SleepToWeekFragment.newInstance())
        fragments.add(SleepToMonthFragment.newInstance())
        //初始化viewpager2
        view_pager.initToActivity(this, fragments)
        //初始化 magic_indicator
        magic_indicator.bindViewPager2(view_pager, mDataList)
//        mViewModel.getSleepData()

        rv_to7day.layoutManager = GridLayoutManager(this, 7)
    }

    override fun layoutId(): Int = R.layout.activity_sleep

    override fun showLoading(message: String) {
    }

}